package com.yc.cloud.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.service.SysDictService;
import com.yc.cloud.api.service.SysDictCacheService;
import com.yc.cloud.basic.constants.RedisKeyConstants;
import com.yc.cloud.basic.dto.BaseQueryParam;
import com.yc.cloud.entity.SysDict;
import com.yc.cloud.mapper.SysDictMapper;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author Yancy
 * @since 2024-01-18
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    private final SysDictCacheService sysDictCacheService;

    public SysDictServiceImpl(SysDictCacheService sysDictCacheService) {
        this.sysDictCacheService = sysDictCacheService;
    }


    @Override
    public IPage<SysDict> list(BaseQueryParam queryParam) {
        val query = new QueryWrapper<SysDict>();
        IPage<SysDict> page;
        //排序
        if (StrUtil.isNotEmpty(queryParam.getSortBy())) {
            if (BaseQueryParam.ORDER_DESC.equals(queryParam.getOrder())) {
                query.orderByDesc(SysDict.ID);
            } else if (BaseQueryParam.ORDER_ASC.equals(queryParam.getOrder())) {
                query.orderByAsc(SysDict.ID);
            }
        }
        //模糊查询
        if (StrUtil.isNotEmpty(queryParam.getKeyword())) {
            query.and(queryWrapper -> {
                queryWrapper.like(SysDict.DICT_TEXT, queryParam.getKeyword()).or().like(SysDict.DICT_VALUE, queryParam.getKeyword());
            });
        }
        //分页
        if (queryParam.getPage()) {
            page = new Page<>(queryParam.getPageNum(), queryParam.getPageSize(), true);
        } else {
            page = new Page<>(queryParam.getPageNum(), baseMapper.selectCount(query), true);
        }
        return baseMapper.selectPage(page, query);
    }

    @Override
    public SysDict create(SysDict param) {
        val sysdict = new SysDict();
        BeanUtils.copyProperties(param, sysdict);
        val saveFlag = save(sysdict);
        if (saveFlag) {
            val key = RedisKeyConstants.SYS_DICTS + ":" + sysdict.getDictText();
            sysDictCacheService.setByKey(key, sysdict);
        }
        return saveFlag ? sysdict : null;
    }

    @Override
    public SysDict get(Long id) {
        return getById(id);
    }

    @Override
    public boolean delete(Long id) {
        val sysDict = getById(id);
        if (sysDict != null) {
            val flag = removeById(id);
            if (flag) {
                deleteCache(sysDict);
            }
        }
        return true;
    }

    @Override
    public SysDict update(Long id, SysDict param) {
        val sysdict = new SysDict();
        BeanUtils.copyProperties(param, sysdict);
        sysdict.setId(id);
        val updateFlag = updateById(sysdict);
        if (updateFlag) {
            deleteCache(sysdict);
        }
        return updateFlag ? sysdict : null;
    }


    @Override
    public SysDict convertVo(SysDict sysdict) {
        if (sysdict == null) {
            return null;
        }
        val vo = new SysDict();
        BeanUtils.copyProperties(sysdict, vo);
        return vo;
    }

    private void deleteCache(SysDict sysDict) {
        val key = RedisKeyConstants.SYS_DICTS + ":" + sysDict.getDictText();
        sysDictCacheService.delByKey(key);
    }
}
